package core.Resource
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Loader;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	import flash.net.URLRequest;
	import flash.system.ApplicationDomain;
	import flash.system.LoaderContext;

	public class DressPack extends BasePack
	{
		//public var name:String = "";
		/**
		 * 记录状态，-1：未使用，0：已开始读取，1：已完成读取
		 * */
		//public var state:int = -1;
		
		private var _data:Array;
		private var _resource:Bitmap;
		private var _splitedResouce:Array;
		private var _loader:Loader;
		
		public function DressPack()
		{
			_splitedResouce = [];
			_loader = new Loader();
			_loader.contentLoaderInfo.addEventListener(Event.COMPLETE,completeHandler);
		}
		
		public override function load(packName:String,url:String):void
		{
			name = packName;
			var srcPackageContext:LoaderContext = new LoaderContext();
			srcPackageContext.applicationDomain= new ApplicationDomain();
			_loader.load(new URLRequest(url),srcPackageContext);
			state = 0;
		}
		
		public function getResource(partName:String):BitmapData
		{
			if(_splitedResouce[partName]!=null)
				return _splitedResouce[partName];
			else
			{
				if(_data[partName]!=null)
				{
					var originX:int =  int(_data[partName][0]);
					var originY:int =  int(_data[partName][1]);
					var width:int =  int(_data[partName][2]);
					var height:int =  int(_data[partName][3]);
					var rect:Rectangle = new Rectangle(originX,originY,width,height);
					var bmd:BitmapData = new BitmapData(width,height,true,0x0000000);
					bmd.setVector(bmd.rect,_resource.bitmapData.getVector(rect));
					_splitedResouce[partName] = bmd;
					return bmd;
				}
				else
					return null;
			}
		}
		
		public function getOffset(partName:String):Point
		{
			if(_data[partName]!=null)
				return new Point(int(_data[partName][4]),int(_data[partName][5]))
			else
				return null;
		}

		public override function reset():void
		{
			name = "";
			state = -1;
			if(_data!=null)
				_data = null;
			if(_resource!=null)
			{
				_resource.bitmapData.dispose();
				_resource = null;
			}
			if(_splitedResouce!=null)
			{
				for each(var bmd:BitmapData in _splitedResouce)
				{
					bmd.dispose();
				}
				_splitedResouce = [];
			}
			
			
		}
		public override function dispose():void
		{
			reset();
			if(_loader!=null)
			{
				_loader.contentLoaderInfo.removeEventListener(Event.COMPLETE,completeHandler);
				_loader = null
			}
		}
		
		private function completeHandler(e:Event):void
		{
			if(!e.target.applicationDomain.hasDefinition("SourceDefinitions"))
				return;
			
			var packClass:Class = e.target.applicationDomain.getDefinition("SourceDefinitions") as Class;
			var packInstance:Object=new packClass();
			
			var key:String = "";
				
			var resourceClass:Class = packInstance.mapDataSource;
			_resource = new resourceClass();
			
			_data = packInstance.sourceArray;
			state = 1;
			this.dispatchEvent(new Event(Event.COMPLETE));

		}
	}
}